﻿
using Controller.i18n.entity;
using HBase;
using hCommand;
using hconnect;
using Persistence;
using System.Collections.Generic;

namespace Controller
{
    public class ClearCarrierTask : ZtTask
    {
        public ClearCarrierTask()
        {
            time = 1000;
        }
        protected IWCSServer wcsServer = WMSDalFactray.getDal<IWCSServer>();

        protected List<PlcCarrierPoint> plcCarrierPoints = new List<PlcCarrierPoint>();
        protected override bool init()
        {
            plcCarrierPoints.Add(PlcCarrierPoint.getPlcCarrierPoint(12));
            plcCarrierPoints.Add(PlcCarrierPoint.getPlcCarrierPoint(22));
            plcCarrierPoints.Add(PlcCarrierPoint.getPlcCarrierPoint(33));
            plcCarrierPoints.Add(PlcCarrierPoint.getPlcCarrierPoint(43));
            return true;
        }
        public override bool excute()
        {
            foreach (var it in plcCarrierPoints)
            {
                clear(it);
            }
            return true;
        }

        void clear(PlcCarrierPoint it)
        {
            if (it.p == 0)
            {
                return;
            }
            Signal carrierSignal = StatusCache.getSignalStatus<CarrierSignalStatus>(it.p);
            if (it.p == 6)
            {
                it.p = 6;
            }
            if (carrierSignal == null)
            {
                return;
            }
            int taskNO = carrierSignal.no;
            if (taskNO == 0)
            {
                return;
            }
            if (!carrierSignal.isArrived())
            {
                return;
            }
            TaskCarrier taskCarrier = BaseTask.getTaskByTaskNo<TaskCarrier>(taskNO);

            if (taskCarrier == null)
            {
                ClearTask(it);
                fileAll(12, "托盘号为 " + taskNO + "的出入库任务不存在！ ");
                return;
            }
            int path = it.p;
            if (taskCarrier.des != path)
            {
                return;
            }
            if (taskCarrier.status != 9)
            {
                return;
            }
            TaskCrane taskCrane = BaseTask.getNextTaskByTaskNo<TaskCarrier, TaskCrane>(path, taskCarrier.taskNo);
            if (taskCrane != null)
            {
                if (taskCrane.status >= (int)CoreTaskParamStatus.取货完成)
                {
                    ClearTask(it);
                }
            }
            else
            {
                ClearTask(it);
            }

        }




        protected void ClearTask(PlcCarrierPoint point)
        {

            TaskPool.get<CarrierConnect1>().clearCarrierTask(point.WOffset);
        }

    }
}
